﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CuaHangLego.Bussiness;
using System.Windows.Forms.DataVisualization.Charting;

namespace CuaHangLego.UI
{
    public partial class QLDoanhThuUI : UserControl
    {
        HoaDonBSS bss;
        IList<SeriesChartType> ltype;
        public QLDoanhThuUI()
        {
            InitializeComponent();
            tblDoanhThu.AutoGenerateColumns = false;
            this.bss = new HoaDonBSS();
        }


        private void QLDoanhThuUI_Load(object sender, EventArgs e)
        {
            txtTongcong.Text = bss.GetSum().ToString();

            ltype = new List<SeriesChartType>();
            ltype.Add(SeriesChartType.Pie);
            ltype.Add(SeriesChartType.Column);
            ltype.Add(SeriesChartType.Doughnut);
        }

        private void buttonXemDoanhThu_Click(object sender, EventArgs e)
        {
            bss.UpdateData();
            tblDoanhThu.DataSource = bss.GetSanPhamThongKe(calDoanhThuNgayTu.Value,
                calDoanhThuNgayDen.Value,
                (int)txtDoanhThuSoLuongTu.Value, (int)txtDoanhThuSoLuongDen.Value);
            txtTongcong.Text = String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:n0}", bss.GetSum());
            txtTong2.Text = String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:n0}.000", bss.GetSum2());
            VeDoThi();
        }

        private void buttonXemDoanhThuTatCa_Click(object sender, EventArgs e)
        {
            bss.UpdateData();
            tblDoanhThu.DataSource = bss.GetAllSanPhamThongKe();
            txtTongcong.Text = String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:n0}", bss.GetSum());
            txtTong2.Text = String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:n0}.000", bss.GetSum2());
            VeDoThi();
        }

        private void buttonXuatBaoCaoDT_Click(object sender, EventArgs e)
        {
            bss.Export();
        }

        public void VeDoThi()
        {
            var l = bss.GetCurrentCTHDs();
            int[] yValues = (from n in l
                             select n.SoLuongBan).ToArray();
            string[] xValues = (from n in l
                                select n.SanPham.Ten).ToArray();
            cbxLoai.DataSource = ltype;

            Chart1.Series.Clear();
            Chart1.Series.Add("Doanh số");
            Chart1.Series["Doanh số"].Points.DataBindXY(xValues, yValues);
            // Type
            Chart1.Series["Doanh số"].ChartType = (SeriesChartType)cbxLoai.SelectedItem;

            Chart1.Series["Doanh số"]["PieLabelStyle"] = "Disabled";
            // 3D
            Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = checkBox3D.Checked;
            Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Interval = 1;
        }

        private void cbxLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            Chart1.Series["Doanh số"].ChartType = (SeriesChartType)cbxLoai.SelectedItem;
        }

        private void checkBox3D_CheckedChanged(object sender, EventArgs e)
        {
            Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = checkBox3D.Checked;
        }
    }
}
